package com;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.log.Log;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import us.codecraft.xsoup.XElements;
import us.codecraft.xsoup.Xsoup;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DownTxT {

    private final static Log log = Log.get();

    static Set<String> set = new HashSet<>();

    private final static ThreadPoolExecutor executorMiddleService = new ThreadPoolExecutor(
            2, 3,
            60, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>());

    public static void main(String[] args) {
//        final List<File> fileList = FileUtil.loopFiles("C:\\Users\\yunlong.li\\Documents\\WeChat Files\\wxid_qz464g33wrhf21\\FileStorage\\Fav\\Temp\\3d43c67a\\res\\c8922f4571ac973e6ae5cf4356d37e93\\cmsblogs\\www.cmsblogs.com\\article");
////        http(url, "163801067875232", true);
//        fileList.forEach(file -> {
//            final String readString = FileUtil.readString(file, Charset.defaultCharset());
//            http(readString);
//        });
        File file = new File("C:\\Users\\yunlong.li\\Desktop\\tsxs2.txt");
        String host = "http://m.luoqiuzww.cc";

        parse("http://m.luoqiuzww.cc/novel/62165/read_62828972.html", host, file);
    }


    public static void parse(String url, String host, File file){
        try {
            HttpRequest httpRequest = HttpRequest.get(url);

            final String soap = httpRequest.timeout(30000).execute().body();

            Document document = Jsoup.parse(soap);
            final String nextPagePath = Xsoup.select(document, "/html/body/div[1]/div[2]/div/div[3]/a/@href").get();
            final XElements xElements = Xsoup.select(document, "//*[@id=\"pt-pop\"]/p/text()");
            xElements.list().forEach(strs -> {
                System.out.println(strs);
                FileUtil.appendString(strs + "\n", file, Charset.defaultCharset());
            });
            Thread.sleep(1000);
            DownTxT.parse(host + nextPagePath, host, file);
        } catch (Exception e) {
            System.out.println(url);
        }

    }


    public static void http(String soap){

        Document document = Jsoup.parse(soap);

        List<String> nextUrl = Xsoup.select(document,"//*/img/@tppabs").list();
        String fileBase = "C:\\Users\\yunlong.li\\Documents\\WeChat Files\\wxid_qz464g33wrhf21\\FileStorage\\Fav\\Temp\\3d43c67a\\res\\c8922f4571ac973e6ae5cf4356d37e93\\cmsblogs\\image.cmsblogs.com";
        nextUrl.parallelStream().forEach(url -> {
            if (url.contains("http://image.cmsblogs.com")) {
                final String substring = url.substring("http://image.cmsblogs.com".length());
                if (!FileUtil.file(fileBase + substring).exists()) {
                    try {
                        HttpUtil.downloadFile(url, FileUtil.file(fileBase + substring));
                        log.info("文件下载 {}", fileBase + substring);
                    } catch (Exception e) {
                        log.error("| {}, e", fileBase + substring, e);
                    }

                } else {
                    log.info("文件已存在 {}", fileBase + substring);
                }
            }
        });

//
//
//        for (String item : arr) {
//            if (item.contains("<title>")) {
//                title = item.substring(item.indexOf("<title>")+7, item.lastIndexOf("</title"));
//                title = title.trim();
//                File titleFile = new File(String.format(filUrl,title,0));
//                if (!titleFile.getParentFile().exists()) {
//                    titleFile.getParentFile().mkdirs();
//                }
//            }
//            if (item.contains("<img src=\"")) {
//                String imgUrl = item.substring(item.indexOf("src=\"") + 5, item.lastIndexOf("\""));
//                if (!imgUrl.contains("\"")){
//                    if (bool) {
//                        try {
//                            String name = id+ "-"+ String.format("%03d", index);
//                            if (!FileUtil.file(String.format(filUrl,title,name)).exists()) {
//                                HttpUtil.downloadFile(imgUrl, FileUtil.file(String.format(filUrl,title,name)));
//                                log.info("{} {}", imgUrl, title);
//                            }
//                        } catch (Exception e) {
//                            log.info(e.getMessage());
//                        }
//                    }
//                    index++;
//                }
//            }
//            if (item.contains("onclick=\"jump('")) {
//                String  urlNum = item.substring(item.indexOf("t=") + 2, item.indexOf("')\">"));
//                if (index == 0) {
//                    log.error(title);
//                    List<String> strings = test(str);
//                    if (!ObjectUtils.isEmpty(strings)){
//                        for (String url : strings){
//                            if (bool) {
//                                try {
//                                    String name = id+ "-"+ String.format("%03d", index);
//                                    if (!FileUtil.file(String.format(filUrl,title,name)).exists()) {
//                                        HttpUtil.downloadFile(url, FileUtil.file(String.format(filUrl,title,name).trim()));
//                                        log.info("{} {}", url, title);
//                                    }else {
//                                        log.info("文件已存在 {}", String.format(filUrl,title,name));
//                                    }
//                                } catch (Exception e) {
//                                    log.info(e.getMessage());
//                                }
//                            }
//                            index++;
//                        }
//                    }
//                }
//                Boolean addBool = set.add(urlNum);
//                executorMiddleService.execute(() ->
//                        http("http://youke.cn-idvhliy.qiniudns.com.zhaowho.cn/qq.php?t=" + urlNum, urlNum, addBool)
//                );
//            }
//
//        }
    }

    public static  List<String>  test(String item){
        Pattern p=Pattern.compile("src=\"(.*?)0\"");
        Matcher m=p.matcher(item);
        List<String> urls = new ArrayList<>();
        while(m.find()){
            urls.add(m.group(1) + "0");
        }
        return urls;
    }
}
